<template>
  <div>
    <div class="flex justify-between items-center mb-6">
      <h2 class="text-2xl font-bold">用户管理</h2>
      <!-- 添加搜索框 -->
      <div class="flex items-center">
        <el-input
          v-model="searchKeyword"
          placeholder="请输入用户名搜索"
          clearable
          @keyup.enter="searchUsers"
        ></el-input>
        <el-button type="primary" @click="searchUsers">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
        <el-button type="primary" @click="openAddDialog">
          <el-icon><Plus /></el-icon>
          添加用户
        </el-button>
      </div>
    </div>
    <el-table :data="filteredUsers" :border="true">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="nickname" label="昵称"></el-table-column>
      <el-table-column prop="avatar" label="头像">
        <template #default="{ row }">
          <img src="https://picsum.photos/50/50" alt="头像" style="width: 40px; height: 40px; border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="birthday" label="生日"></el-table-column>
      <el-table-column prop="qq" label="qq"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="role" label="权限">
        <template #default="{ row }">
          <el-tag v-if="row.role === 'admin'" type="success">管理员</el-tag>
          <el-tag v-else>普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" @click="openEditDialog(row)">
            <el-icon><Edit /></el-icon>
            编辑
          </el-button>
          <el-button type="danger" @click="deleteUser(row)">
            <el-icon><Delete /></el-icon>
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加用户对话框 -->
    <el-dialog :visible.sync="addDialogVisible" title="添加用户">
      <template #content>
        <el-form :model="addForm" ref="addFormRef" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addUser">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑用户对话框 -->
    <el-dialog :visible.sync="editDialogVisible" title="编辑用户">
      <template #content>
        <el-form :model="editForm" ref="editFormRef" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="editForm.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateUser">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Search, Plus, Edit, Delete } from '@element-plus/icons-vue';

// 用户列表
const users = ref([
  { id: 1,nickname: '晓晓', qq: '12316435',birthday: '',sender: 0, username: 'admin', email: 'admin@example.com',role: 'admin'  },
  { id: 2,nickname: '周庄',qq: '12316435',birthday: '',sender: 1, username: 'user1', email: 'user1@example.com',role: 'admin'  }
]);

// 搜索关键词
const searchKeyword = ref('');
// 过滤后的用户列表
const filteredUsers = ref([...users.value]);

// 添加用户对话框相关
const addDialogVisible = ref(false);
const addForm = ref({
  username: '',
  email: ''
});
const addFormRef = ref(null);

// 编辑用户对话框相关
const editDialogVisible = ref(false);
const editForm = ref({
  id: null,
  username: '',
  email: ''
});
const editFormRef = ref(null);

// 搜索用户
const searchUsers = () => {
  if (searchKeyword.value) {
    filteredUsers.value = users.value.filter(user =>
      user.username.includes(searchKeyword.value)
    );
  } else {
    filteredUsers.value = [...users.value];
  }
};

// 打开添加用户对话框
const openAddDialog = () => {
  addDialogVisible.value = true;
  addForm.value = {
    username: '',
    email: ''
  };
};

// 添加用户
const addUser = () => {
  const newId = users.value.length > 0 ? users.value[users.value.length - 1].id + 1 : 1;
  const newUser = {
    id: newId,
    username: addForm.value.username,
    email: addForm.value.email
  };
  users.value.push(newUser);
  filteredUsers.value = [...users.value];
  addDialogVisible.value = false;
};

// 打开编辑用户对话框
const openEditDialog = (row) => {
  editDialogVisible.value = true;
  editForm.value = {
    id: row.id,
    username: row.username,
    email: row.email
  };
};

// 更新用户信息
const updateUser = () => {
  const index = users.value.findIndex(user => user.id === editForm.value.id);
  if (index !== -1) {
    users.value[index].username = editForm.value.username;
    users.value[index].email = editForm.value.email;
    filteredUsers.value = [...users.value];
  }
  editDialogVisible.value = false;
};

// 删除用户
const deleteUser = (row) => {
  const index = users.value.findIndex(user => user.id === row.id);
  if (index !== -1) {
    users.value.splice(index, 1);
    filteredUsers.value = [...users.value];
  }
};
</script>

<style scoped>
/* 可以根据需要添加样式 */
</style>